package com.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.common.utils.base.UuidUtil;
import com.entity.AttachmentRelationship;
import com.entity.News;
import com.repository.AttaRelationshipDao;
import com.repository.NewsDao;
import com.service.base.BaseServiceImp;
@Service("NewsService")
public class NewsServiceImp extends BaseServiceImp<News> implements NewsService{
	//用户
	@Autowired
	private NewsDao newsdao;

	//附件关系
	@Autowired
	private AttaRelationshipDao fjgxDao;
	//附件
	@Autowired
	private  AttachmentService AttachServ;
	@Override
	public List<News> findByCid(News o) {
		return newsdao.findByCid(o);
	}

	@Transactional//事务回滚
	@Override
	public void insertAndAttid(News news,String AttachmentId) {

		news.setNewsId(UuidUtil.get32UUID());
		news.setCreationTime(new Date());
		String fujianArry[] ;
		if(AttachmentId.contains(",")){
			fujianArry=AttachmentId.split(",");
		}else{
			fujianArry=new String[]{AttachmentId};
		}
		//添加附件关系
		if(AttachmentId!=null){
			for(int i=0;i<fujianArry.length;i++){
				AttachmentRelationship fujiangx=new AttachmentRelationship();
				fujiangx.setAttachmentRelationshipId(UuidUtil.get32UUID());
				fujiangx.setAssociatedId(news.getNewsId());
				fujiangx.setAttachmentId(fujianArry[i]);
				fujiangx.setAssociatedName("news");
				fjgxDao.insert(fujiangx);
			}
		}
		newsdao.insert(news);
	}
}
